<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="出货时间" prop="shipmentDate">
        <el-date-picker
          v-model="formData.shipmentDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择出货时间"
        />
      </el-form-item>
      <el-form-item label="客户" prop="customer">
        <el-input v-model="formData.customer" placeholder="请输入客户" />
      </el-form-item>
      <el-form-item label="票号" prop="ticketNumber">
        <el-input v-model="formData.ticketNumber" placeholder="请输入票号" />
      </el-form-item>
      <el-form-item label="厂内编号" prop="internalNumber">
        <el-input v-model="formData.internalNumber" placeholder="请输入厂内编号" />
      </el-form-item>
      <el-form-item label="型号（KP）" prop="modelKp">
        <el-input v-model="formData.modelKp" placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="FLEX" prop="flex">
        <el-input v-model="formData.flex" placeholder="请输入FLEX" />
      </el-form-item>
      <el-form-item label="打击板结构" prop="hammerPlateStructure">
        <el-input v-model="formData.hammerPlateStructure" placeholder="请输入打击板结构" />
      </el-form-item>
      <el-form-item label="打击板模号" prop="hammerPlateModel">
        <el-input v-model="formData.hammerPlateModel" placeholder="请输入打击板模号" />
      </el-form-item>
      <el-form-item label="方向" prop="direction">
        <el-input v-model="formData.direction" placeholder="请输入方向" />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input v-model="formData.quantity" placeholder="请输入数量" />
      </el-form-item>
      <el-form-item label="长度" prop="productLength">
        <el-input v-model="formData.productLength" placeholder="请输入长度" />
      </el-form-item>
      <el-form-item label="T/C/F" prop="tcf">
        <el-input v-model="formData.tcf" placeholder="请输入T/C/F" />
      </el-form-item>
      <el-form-item label="ST1271" prop="st1271">
        <el-input v-model="formData.st1271" placeholder="请输入ST1271" />
      </el-form-item>
      <el-form-item label="是否待定" prop="bat">
        <el-select
          v-model="formData.bat"
          placeholder="请选择是否待定"
          clearable
          class="!w-240px"
        >
          <el-option label="是" value="是" />
          <el-option label="否" value="否" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DispatchListApi, DispatchListVO } from '@/api/hcxly/dispatchList/Index'
import { formatDateArray } from '@/utils/formatTime'

/** 派工单 表单 */
defineOptions({ name: 'DispatchListForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  shipmentDate: undefined,
  customer: undefined,
  ticketNumber: undefined,
  internalNumber: undefined,
  modelKp: undefined,
  flex: undefined,
  hammerPlateStructure: undefined,
  hammerPlateModel: undefined,
  direction: undefined,
  quantity: undefined,
  productLength: undefined,
  tcf: undefined,
  st1271: undefined,
  bat: undefined,
  remark: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DispatchListApi.getDispatchList(id)
      formData.value.shipmentDate = formatDateArray(formData.value.shipmentDate);
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DispatchListVO
    if (formType.value === 'create') {
      await DispatchListApi.createDispatchList(data)
      message.success(t('common.createSuccess'))
    } else {
      await DispatchListApi.updateDispatchList(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}


/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    shipmentDate: undefined,
    customer: undefined,
    ticketNumber: undefined,
    internalNumber: undefined,
    modelKp: undefined,
    flex: undefined,
    hammerPlateStructure: undefined,
    hammerPlateModel: undefined,
    direction: undefined,
    quantity: undefined,
    productLength: undefined,
    tcf: undefined,
    st1271: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
